import Platform
import pygame
image1=pygame.image.load("wippe.jpg")
image2=pygame.image.load("wippe_.jpg")
image1.set_colorkey((137,44,75))

class Wippe(Platform.Platform):
	def __init__(self,pos=(0,0),image=image1,image_=image2):
		Platform.Platform.__init__(self,pos=pos,image=image)
		self.image_=image2
		self.a=0
		self.k=0
		self.mlk=-float(self.image_.get_height())/(-self.image.get_width())
		self.mrk=-float(self.image_.get_height())/(self.image.get_width())
	def xLanding(self,objecto):
		#x_l=objecto.intermediate_x()
		x1=self.leftSide()
		x2=self.rightSide()
		if objecto.prev_x<=x1<=objecto.x and objecto.y<=self.upperSide(x=x1)<=objecto.y+objecto.getHeight():
			objecto.rebound(x1-0.5*objecto.getWidth(),objecto.y)
			if objecto.isAlive():
				objecto.facingRight=True
				self.boundX(objecto,True)
		elif objecto.x<=x2<=objecto.prev_x and objecto.y<=self.upperSide(x=x2)<=objecto.y+objecto.getHeight():
			objecto.rebound(x2+0.5*objecto.getWidth(),objecto.y)
			if objecto.isAlive():
				objecto.facingRight=False
				self.boundX(objecto,True)
	def yLanding(self,objecto):
		x=objecto.x
		y=self.upperSide(x=objecto.x)
		if self.within_x_boundary(x,0) and objecto.y<=y<=objecto.prev_y+objecto.getHeight():
			p=objecto.rebound(x,y)
			self.boundY(objecto,False)
			r=((self.pos[1]+self.image_.get_height()-y)**2+(self.pos[0]-x)**2)**0.5
			if x>self.pos[0] and y>=self.image_.get_height()+self.pos[1]:
				self.knock_up_lefts(p*r)
			elif x<self.pos[0] and y>=self.image_.get_height()+self.pos[1]:
				self.knock_up_rights(p*r)
	def upperSide(self,x=0):
		return self.k*(x-self.pos[0])+self.pos[1]+self.image_.get_height()
	def process(self):
		import math
		lm=0
		rm=0
		for obj in self.boundedList:
			if obj.x<self.pos[0]:
				r=((self.pos[0]-obj.x)**2+(self.pos[1]+self.image_.get_height()-obj.y)**2)**0.5
				lm=lm+r*obj.getMass()
			elif obj.x>self.pos[0]:
				r=((self.pos[0]-obj.x)**2+(self.pos[1]+self.image_.get_height()-obj.y)**2)**0.5
				rm=rm+r*obj.getMass()
		print "masses",lm,rm
		if lm==rm:
			#self.a=0
			self.k=0
		elif lm>rm:
			checked=False
			if rm!=0:
				self.k=lm/rm
			else:
				self.k=self.mlk
				checked=True
			if not checked and self.upperSide(x=self.pos[0]-self.image.get_width())<self.pos[1]:
				self.k=self.mlk
		else:
			checked=False
			if lm!=0:
				self.k=-rm/lm
			else:
				self.k=self.mrk
				checked=True
			if not checked and self.upperSide(x=self.pos[0]+self.image.get_width())<self.pos[1]:
				self.k=self.mrk
		self.a=math.atan(self.k)
		print self.a,self.k
	def leftSide(self):
		x1=-0.5*self.image.get_width()
		if self.a!=0:
			import math
			x1=x1*math.cos(self.a)
		return x1+self.pos[0]
	def rightSide(self):
		x2=0.5*self.image.get_width()
		if self.a!=0:
			import math
			x2=x2*math.cos(self.a)
		return x2+self.pos[0]
	def within_x_boundary(self,ox,ow):
		#
		x1=-0.5*self.image.get_width()
		x2=0.5*self.image.get_width()
		if self.a!=0:
			import math
			x1=x1*math.cos(self.a)
			x2=x2*math.cos(self.a)
		return x1<=ox-self.pos[0]<=x2
	def drawingInformation(self):
		import math
		print "///",self.a
		im1=pygame.transform.rotate(self.image,self.a*180/math.pi)
		h=0.5*im1.get_height()
		#if h/2>self.image_.get_height():
		#	h=h-h/2+self.image_.get_height()
		h=h+self.image_.get_height()#-0.5*self.image.get_height()
		surf=pygame.Surface((im1.get_width(),h))
		surf.blit(im1,(0,0))
		surf.blit(self.image_,[0.5*surf.get_width()-0.5*self.image_.get_width(),surf.get_height()-self.image_.get_height()])
		return [self.pos[0]-0.5*surf.get_width(),self.pos[1]+surf.get_height()],surf
	def hasFallenOff_x(self,objecto):
		X=self.rightSide() if objecto.x>self.pos[0] else self.leftSide()
		U=self.upperSide(x=X)
		if objecto.y+objecto.getHeight()-U>=objecto.getCrawlHeight():
			if objecto.y_bound:
				objecto.y_bound.unbound(objecto)
			#yb=self.x_bound
			self.unbound(objecto)
			objecto.crawl=True
			objecto.reposition(X,U)
			self.boundY(objecto,False)
			#pass
	def hasFallenOff_y(self,objecto):
		x=objecto.x
		y=self.upperSide(x=objecto.x)
		my=self.upperSide(x)
		if objecto.y!=my:
			objecto.y=my
			#self.unbound(objecto)
		if not self.within_x_boundary(x,0):
			self.unbound(objecto)
	def knock_up_lefts(self,moment):
		summ_rm=0
		#summ_m=0
		inclu=[]
		for obj in self.boundedList:
			if obj.x<self.pos[0]:
				m=obj.getMass()
				r=((self.pos[0]-obj.x)**2+(self.pos[1]+self.image_.get_height()-obj.y)**2)**0.5
				summ_rm=summ_rm+r*m
				inclu.append(obj)
		if summ_rm>0:
			V=moment/summ_rm
			c=(1+self.k*self.k)**0.5
			c1=1/c
			c2=self.k/c
			for obj in inclu:
				obj.coordDump()
				obj.y=obj.y+c1*V*1.0/24
				obj.x=obj.x+c2*V*1.0/24
				self.unbound(obj)
	def knock_up_rights(self,moment):
		summ_rm=0
		#summ_m=0
		inclu=[]
		for obj in self.boundedList:
			if obj.x>self.pos[0]:
				m=obj.getMass()
				r=((self.pos[0]-obj.x)**2+(self.pos[1]+self.image_.get_height()-obj.y)**2)**0.5
				summ_rm=summ_rm+r*m
				inclu.append(obj)
		if summ_rm>0:
			V=moment/summ_rm
			c=(1+self.k*self.k)**0.5
			c1=1/c
			c2=self.k/c
			for obj in inclu:
				obj.coordDump()
				obj.y=obj.y+c1*V*1.0/24
				obj.x=obj.x+c2*V*1.0/24
				self.unbound(obj)
	def pointOfPlatform(self,x,y):
		return False